package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.dao.McarParkingMapper;
import com.woniuxy.dao.MerchantCodeMapper;
import com.woniuxy.dao.MerchantUsersMapper;
import com.woniuxy.dao.UserMapper;
import com.woniuxy.dto.DataGridView;
import com.woniuxy.pojo.*;
import com.woniuxy.service.UserService;
import com.woniuxy.vo.McarParkingVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class UserSercicemlpl implements UserService {
    @Autowired
    private MerchantUsersMapper merchantUsersMapper;
    @Autowired
    private McarParkingMapper mcarParkingMapper;
    @Autowired
    private org.springframework.core.env.Environment environment;
    @Autowired
    private MerchantCodeMapper merchantCodeMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public Integer selectCarNums(Integer pid) {
        MerchantUsers merchantUsers =findPuserByid(pid);
        return merchantUsersMapper.selectCarNums(merchantUsers.getId());
    }

    @Override
    public Integer selectCarNumson(Integer pid) {
        MerchantUsers merchantUsers =findPuserByid(pid);
        return merchantUsersMapper.selectCarNumson(merchantUsers.getId());
    }

    @Override
    public Integer selectCarNumsdo(Integer pid) {
        MerchantUsers merchantUsers =findPuserByid(pid);
        return merchantUsersMapper.selectCarNumsdo(merchantUsers.getId());
    }

    @Override
    public DataGridView findAllcarsBypid (McarParkingVo mcarParkingvo) {

        Page<McarParking> page= PageHelper.startPage(mcarParkingvo.getPage(),mcarParkingvo.getLimit());
        List<McarParking> x =mcarParkingMapper.findAllcarsBypid(mcarParkingvo);
        return new DataGridView(page.getTotal(), x);
    }

    @Override
    public void setParkingState(Integer id, String state) {
        mcarParkingMapper.setParkingState(state,id);
    }

    @Override
    public void setParkinguState(Integer id, String uState) {
        mcarParkingMapper.setParkinguState( id, uState);
    }

    @Override
    public void setParkingpState(Integer id, String pState) {
    mcarParkingMapper.setParkingpState(id,pState);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addpUser(MerchantUsers users) {
        users.setRegtime(new Date());
        merchantUsersMapper.addpUser(users);
        User user=new User();
        user.setUsername(users.getmPhonenum());
        boolean flag=false;
        String algorithmName="MD5";
        Object salt=null;
        int hashIterations=1024;
        Object encryptionPwd=new SimpleHash(algorithmName,users.getmPassword(),salt,hashIterations);
        user.setPassword(encryptionPwd.toString());
        user.setRenamezz(users.getmName());
        userMapper.adduser(user);
        //插入中间表
        User user1 =userMapper.finduserByUsername(user);
        MerchantUsers merchantUsers=merchantUsersMapper.findMerchantuser(users);
        //插入用户角色
        userMapper.insertUserRole(user1);
        System.out.println(user1.getId()+merchantUsers.getId());
        //插入真假用户表
        UserMerchant userMerchant=new UserMerchant();
        userMerchant.setUid(user1.getId());
        userMerchant.setPid(merchantUsers.getId());
        merchantUsersMapper.insertUM(userMerchant);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(MerchantAuth merchantUsers) {
        merchantUsersMapper.register(merchantUsers);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Integer id) {
        mcarParkingMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(Integer[] ids) {
        for (Integer id : ids) {
            deleteById(id);
        }

    }

    @Override
    public User findUserByTel(String mPhonenum) {
        return merchantUsersMapper.findUserByTel(mPhonenum);
    }

    /**
     * \通过user id找merchantUser
     * @param id
     * @return
     */
    @Override
    public MerchantUsers findPuserByid(Integer id) {
        return merchantUsersMapper.findPuserByid(id);
    }

    /**
     * 往数据库中插入数据
     * @param merchantCode
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addcode(MerchantCode merchantCode) {
       MerchantCode m =merchantCodeMapper.findBytelephone(merchantCode.getTelephone());
       if (m!=null){
           merchantCodeMapper.deleteBytelephone(merchantCode.getTelephone());
       }
       merchantCodeMapper.addcode(merchantCode);
    }

    /**
     * 从数据库中获取验证码
     * @param telephone
     * @return
     */
    @Override
    public String findCodeBytelephone(String telephone) {
        MerchantCode merchantCode=new MerchantCode();
         Date date=new Date();
        Calendar c=Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE,-10);
        date=c.getTime();
        merchantCode.setCreatetime(date);
        merchantCode.setTelephone(telephone);
        return merchantCodeMapper.findCodeBytelephone(merchantCode);
    }

    /**
     * 修改密码
     * @param telephone
     * @param password
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uppassword(String telephone, String password) {
       User user=new User();
       user.setUsername(telephone);
       user.setPassword(password);
        userMapper.uppassword(user);
    }

    @Override
    public void addcar(McarParking mcarParking) {
        MerchantUsers merchantUsers =findPuserByid(mcarParking.getPid());
        mcarParking.setPid(merchantUsers.getId());
        mcarParkingMapper.addcar(mcarParking);
    }
}
